home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / doors_1 / doorskl3.zip / DOORANSI.C < prev    next >
C/C++ Source or Header  |  1991-12-15  |  13KB  |  425 lines

  1. /*
  2.  * Special ANSI input functions (advanced functions; beginners take care)
  3.  * note I've included but not tested these in association with doorskel
  4.  * itself (they were lifted whole from XDB, so they do work).
  5.  * these functions will behave differently if the user doesn't have
  6.  * graphics capabilities, but they'll get by...
  7.  */
  8.  
  9. #include "doorskel.h"
  10.  
  11.  
  12.  
  13.  
  14. char _fastcall ansimenu2 (char tcolor,char color,char *title,
  15.                           char *lineorig,char variable[10][133]) {
  16.  
  17.     /* horizontal framed menu across top of screen selectable by highlighting
  18.        further menu may be pulled down from this menu.  colors are ANSI
  19.        numbers (37 = white).  tcolor is the title/frame color, color is the
  20.        text color of the menus, title is the text for the title in the frame,
  21.        lineorig contains options separated by spaces (use _ within options).
  22.        the variable array contains options like lineorig, one for each
  23.        pull-down menu.  this interfaces to doorhelp.c in a weird but wondrous
  24.        manner :-) by setting the current topic to the first 8 chars of the
  25.        current option and calling help().  Notice that help can't be context-
  26.        sensitive if the user doesn't have graphics capability and this function
  27.        has to emulate a pull-down as a tree-structure...
  28.        for further info you're referred to the XDB source.  This is an advanced
  29.        function and you should avoid it until you feel braver...
  30.  
  31.        Example:
  32.  
  33.         strcpy(var[0]," Help");
  34.         strcpy(var[1],"Select");
  35.         if(gooduser)strcat(var[1]," Create Remove");
  36.         if(sysopuser)strcat(var[1]," New_Index");
  37.         strcat(var[1]," Quit");
  38.         strcpy(var[2],"Key Fields Quit");
  39.         strcpy(var[3],"Field_Names Graphics List_Header Quit");
  40.         strcpy(var[4],"Record_Fields Index_Fields Stats Filters Quit");
  41.         strcpy(var[5],"List Dump Quit");
  42.         strcpy(var[6],"");
  43.         strcpy(var[7],"");
  44.         strcpy(var[8],"");
  45.         strcpy(var[9],"");
  46.         sprintf(s," Database: %s / Index: %-1.38s ",p,index_name(idx));
  47.         strcpy(ss,"Database Index Filter Configure Status Records Quit Help");
  48.         which = ansimenu2(33,36,s,ss,var);
  49.         switch((int)which) {
  50.             ...
  51.     */
  52.  
  53.     register word x,y;
  54.     char nump,numpp;
  55.     char *p[10],*pp[10];
  56.     static char *line = NULL,s[144];
  57.     char helpstring[24];
  58.     word pos[10],ppos[10],pdown,start,temp1,temp2;
  59.     char *t,was,is,temp;
  60.  
  61.     /* Initially display top bar */
  62.  
  63. ReStart:
  64.  
  65.     if(graphics) cls();
  66.     else printm("\n");
  67.     if(line)free(line);
  68.     line = strdup(lineorig);
  69.     if(!graphics) {
  70.         printfm("\n%s\n",title);
  71.     }
  72.     else {
  73.         printfm("\x1b[1;1H\x1b[K\x1b[0;1;%hum╔",color);
  74.         for(x=1;x<((79-strlen(title))/2);x++) printm("═");
  75.         printfm("\x1b[0;1;%hum%s\x1b[0;1;%hum",tcolor,title,color);
  76.         curr_cursor(&temp1,&temp2);
  77.         y = temp1;
  78.         for(x = y;x<79;x++) printm("═");
  79.         printm("╗\n");
  80.         curr_cursor(&temp1,&start);
  81.     }
  82.     stripcr(line);
  83.     if (!*line) return 0;
  84.     x=0;
  85.     p[x++]=strtok(line," ");
  86.     while (x<10 && (p[x++]=strtok(0," ")));
  87.     p[--x]=NULL;
  88.     if (!*p) return 0;
  89.     nump=(char)x-1;
  90.     for (x=0;p[x];x++) while (t=strchr(p[x],'_')) *t=' ';
  91. Over:   /* Redisplay top bar */
  92.     is=was=0;
  93.     for (x=0;x<9;x++) pos[x]=0;
  94.     if(graphics) {
  95.         printfm("\x1b[0;1;%hum",color);
  96.         if(baud)while (!fossil(TRANSMIT,'\r')) carrchk();
  97.         fputs("\r",stdout);
  98.         printm("\x1b[2;1H\x1b[K║  ");
  99.     }
  100.     *pos=3;
  101.     if(!graphics) {
  102.         for (x = 0;p[x];x++) {
  103.             curr_cursor(&temp1,&temp2);
  104.             if((temp1 + strlen(p[x]) + 2 + 2) > 77) printm("\n");
  105.             printfm("[%c]%s  ",*p[x],&p[x][1]);
  106.         }
  107.         printm("\b\b: ");
  108.     }
  109.     else {
  110.         printm(*p);
  111.         for (x=1;p[x];x++) {
  112.             printm("  ");
  113.             printm(p[x]);
  114.             pos[x]=pos[x-1]+strlen(p[x-1])+2;
  115.         }
  116.         printm("\x1b[2;79H║\x1b[3;1H\x1b[K╚");
  117.         for(x=1;x<78;x++) printm("═");
  118.         printm("╝\x1b[2;1H");
  119.     }
  120. Next:   /* Move highlight bar */
  121.     if(graphics) {
  122.         if(baud)while (!fossil(TRANSMIT,'\r')) carrchk();
  123.         fputs("\r",stdout);
  124.         printfm("\x1b[%u;1H\x1b[0;1;%hum\x1b[%uC%s",start,color,pos[was],p[was]);
  125.         printfm("\x1b[%u;1H\x1b[7m\x1b[%uC%s\x1b[0;1;%hum",start,pos[is],p[is],color);
  126.     }
  127.     was = is;
  128. None:   /* Wait for something to happen w/ top bar... */
  129.     if(!graphics) {
  130.         temp=*genin(1,0,1,1,ALLL);
  131.         if(temp=='?') {
  132.             strcpy(helpstring,"MAINHELP");
  133.             set_help(helpstring);
  134.             help();
  135.  
  136.             goto ReStart;
  137.         }
  138.         if(temp)goto Direct;
  139.         else goto None;
  140.     }
  141.     temp=*genin(1,0,0,0,ARROWS);
  142.     switch ((int)temp) {
  143.     case '?':sprintf(helpstring,"%-8u",((is+1)*10));
  144.              set_help(helpstring);
  145.              printg("\x1b[15;1H\x1b[K");
  146.              help();
  147.              goto ReStart;
  148.     case 0:
  149.     case DN:
  150.     case '2': goto GotOne;
  151.  
  152.     case UP:
  153.     case LEFT:
  154.     case '8':
  155.     case '4': if (is) is--;
  156.               else is = nump;
  157.               break;
  158.     case RIGHT:
  159.     case '6': if (!p[is + 1]) is = 0;
  160.               else is++;
  161.               break;
  162.     case 11:
  163.     case 27:  cls();
  164.               *s = 0;
  165.               return 0;
  166.     default:
  167. Direct:
  168.               for(x=0;p[x];x++) {       /* Direct select */
  169.                 if(toupper(*p[x])==toupper(temp)) {
  170.                     if(is!=(char)x) {
  171.                         if(graphics) {
  172.                             printfm("\x1b[%u;1H\x1b[0;1;%hum\x1b[%uC%s",start,color,pos[is],p[is]);
  173.                             printfm("\x1b[%u;1H\x1b[7m\x1b[%uC%s\x1b[0;1;%hum",start,pos[x],p[x],color);
  174.                         }
  175.                     }
  176.                     is=(char)x;
  177.                     goto GotOne;
  178.                 }
  179.               }
  180.               if(!graphics) printm("\b \b");
  181.               break;
  182.     }
  183.     goto Next;
  184.  
  185. GotOne:     /* Pull down actions */
  186.  
  187.     if(!graphics)printm("\n");
  188.     if(!*variable[is]) {  /* No pull downs for this one */
  189.         printg("\x1b[5;1H");
  190.         return ((is+1)*10);
  191.     }
  192.     pdown=pos[is];
  193.     if(pdown>60) pdown=60;
  194.     if(graphics)printfm("\x1b[3;%uH╦\x1b[14C╦\x1b[0m\x1b[0;1;%hum\n",pdown-1,color);
  195.     for(x=0;x<9;x++)ppos[x]=0;
  196.     x=0;
  197.     strcpy(s,variable[is]);
  198.     pp[x++]=strtok(s," ");
  199.     while (x<10 && (pp[x++]=strtok(0," ")));
  200.     pp[--x]=NULL;
  201.     if (!*pp) return 0;
  202.     numpp=(char)x-1;
  203.     for (x=0;pp[x];x++) while (t=strchr(pp[x],'_')) *t=' ';;
  204.     if(!graphics) {
  205.         for (x = 0;pp[x];x++) {
  206.             curr_cursor(&temp1,&temp2);
  207.             if((temp1 + strlen(pp[x]) + 2 + 2) > 77) printm("\n");
  208.             printfm("[%c]%s  ",*pp[x],&pp[x][1]);
  209.         }
  210.         printm("\b\b: ");
  211.     }
  212.     else {
  213.         for(x = 0;pp[x];x++) {
  214.             curr_cursor(&temp1,&ppos[x]);
  215.             printfm("\x1b[K\x1b[%u;%uH║ %-12.12s ║\n",ppos[x],pdown - 1,pp[x]);
  216.         }
  217.         printfm("\x1b[%u;%uH\x1b[K╚══════════════╝",ppos[numpp]+1,pdown-1);
  218.     }
  219.     was=y=0;
  220.  
  221. Over1:  /* Loop until something happens */
  222.  
  223.     if(graphics) {
  224.         printfm("\x1b[%u;%uH\x1b[7m %-12.12s \x1b[0;1;%hum",ppos[y],pdown,pp[y],color);
  225.         temp=*genin(1,0,0,0,ARROWS);
  226.     }
  227.     else {
  228.         temp=*genin(1,0,1,1,ALLL);
  229.         if(temp=='?') {
  230.             sprintf(helpstring,"%-8u",((is+1)*10));
  231.             set_help(helpstring);
  232.             help();
  233.             goto ReStart;
  234.         }
  235.         if(temp) goto Direct2;
  236.         else goto Over1;
  237.     }
  238.     switch ((int)temp) {
  239.         case '?':sprintf(helpstring,"%-8u",((is+1)*10)+(y+1));
  240.                  set_help(helpstring);
  241.                  printg("\x1b[15;1H\x1b[K");
  242.                  help();
  243.                  goto ReStart;
  244.         case LEFT:
  245.         case RIGHT:
  246.         case '4':
  247.         case '6': was=is;
  248.                   for(x=0;pp[x];x++) {
  249.                     printfm("\x1b[%u;1H\x1b[K\n",ppos[x]);
  250.                   }
  251.                   printfm("\x1b[%u;1H\x1b[K\n",ppos[numpp]+1);
  252.                   printfm("\x1b[3;%uH═\x1b[14C═",pdown-1);
  253.                   if (temp==1 || temp=='4') {
  254.                     if (is) is--;
  255.                     else is=nump;
  256.                   }
  257.                   else {
  258.                     if (!p[is+1]) is=0;
  259.                     else is++;
  260.                   }
  261.                   printfm("\x1b[%u;1H\x1b[0;1;%hum\x1b[%uC%s",start,color,pos[was],p[was]);
  262.                   goto Next;
  263.         case 0:
  264. KeyPress1:
  265.                   printg("\x1b[15;1H");
  266.                   return (((is+1)*10)+(y+1));
  267.         case UP:
  268.         case '8': printfm("\x1b[%u;%uH\x1b[0;1;%hum %-12.12s ",ppos[y],pdown,color,pp[y]);
  269.                   if (y) y--;
  270.                   else y=numpp;
  271.                   break;
  272.         case DN:
  273.         case '2': printfm("\x1b[%u;%uH\x1b[0;1;%hum %-12.12s ",ppos[y],pdown,color,pp[y]);
  274.                   if (!pp[y+1]) y=0;
  275.                   else y++;
  276.                   break;
  277.         case 11:
  278.         case 27:  for(x=0;pp[x];x++) {
  279.                     printfm("\x1b[%u;1H\x1b[K\n",ppos[x]);
  280.                   }
  281.                   printfm("\x1b[%u;1H\x1b[K\n",ppos[numpp]+1);
  282.                   *s=0;
  283.                   printfm("\x1b[1;1H\x1b[K\x1b[0;1;%hum╔",color);
  284.                   for(x=1;x<((79-strlen(title))/2);x++) printm("═");
  285.                   printfm("\x1b[0;1;%hum%s\x1b[0;1;%hum",tcolor,title,color);
  286.                   curr_cursor(&temp1,&temp2);
  287.                   temp = (char)temp1;
  288.                   for(x = temp;x < 79;x++) printm("═");
  289.                   printm("╗\n");
  290.                   goto Over;
  291.         default:
  292. Direct2:
  293.                   for(x=0;pp[x];x++) {  /* Direct select */
  294.                     if(toupper(*pp[x])==toupper(temp)) {
  295.                         if(y!=x) {
  296.                             if(graphics)printfm("\x1b[%u;%uH\x1b[0;1;%hum %-12.12s ",ppos[y],pdown,color,pp[y]);
  297.                             if(graphics)printfm("\x1b[%u;%uH\x1b[7m %-12.12s \x1b[0;1;%hum",ppos[x],pdown,pp[x],color);
  298.                         }
  299.                         y=x;
  300.                         goto KeyPress1;
  301.                     }
  302.                   }
  303.                   if(!graphics)printm("\b \b");
  304.     }
  305.     goto Over1;
  306. }
  307.  
  308.  
  309.  
  310. char _fastcall ansimenu (char color, char *lineorig) {
  311.  
  312.     /* one-line horizontal menu with options selectable by highlighting
  313.        lineorig contains options separated by spaces (use _ within
  314.        options).  color is an ANSI number (37 = white).  example:
  315.          option = ansimenu(32,"Quit Continue");
  316.        This interfaces to doorhelp.c by setting the current topic to
  317.        the first 8 chars of the option and calling help().
  318.        Returns the first character of the option selected, or 0 if
  319.        aborted by the user.  Note that help can't be context-sensitive
  320.        if the user doesn't have graphics capabilities.
  321.     */
  322.  
  323.     register word x = 0;
  324.     static char *line = NULL;
  325.     char *p[36];
  326.     word pos[36],temp1,temp2;
  327.     char helpstring[24],*t,was,is,nump,temp;
  328.  
  329.  
  330. ReStart:
  331.  
  332.     if(line)free(line);
  333.     line=strdup(lineorig);
  334.     stripcr(line);
  335.     if (!strlen(line)) return 0;
  336.     p[x++]=strtok(line," ");
  337.     while (x<34 && (p[x++]=strtok(0," ")));
  338.     p[--x]=NULL;
  339.     if (!*p) return 0;
  340.     nump=(char)x-1;
  341.     for (x=0;p[x];x++) while (t=strchr(p[x],'_')) *t=' ';;
  342.     printm("\n");
  343.     if (!graphics) goto NoGraph;
  344. Over:
  345.     is=was=0;
  346.     for (x=0;x<35;x++) pos[x]=0;
  347.     printfm("\x1b[0;1;%hum",color);
  348.     if (baud) while (!fossil(TRANSMIT,'\r')) carrchk();
  349.     fputs("\r",stdout);
  350.     *pos=1;
  351.     printm("\x1b[1C");
  352.     printm(*p);
  353.     for (x=1;p[x];x++) {
  354.         printm("  ");
  355.         printm(p[x]);
  356.         pos[x]=pos[x-1]+strlen(p[x-1])+2;
  357.     }
  358. Next:
  359.     if (baud) while (!fossil(TRANSMIT,'\r')) carrchk();
  360.     fputs("\r",stdout);
  361.     printfm("\x1b[0;1;%hum\x1b[%uC",color,pos[was]);
  362.     printm(p[was]);
  363.     if (baud) while (!fossil(TRANSMIT,'\r')) carrchk();
  364.     fputs("\r",stdout);
  365.     printfm("\x1b[7m\x1b[%uC",pos[is]);
  366.     printm(p[is]);
  367.     printfm("\x1b[0;1;%hum",color);
  368.     was = is;
  369. None:
  370.     temp = *genin(1,0,0,0,ARROWS);
  371.     switch((int)temp) {
  372.         case '?':   sprintf(helpstring,"%-8.8s",p[is]);
  373.                     set_help(helpstring);
  374.                     help();
  375.                     goto ReStart;
  376.         case UP:
  377.         case LEFT:
  378.         case '8':
  379.         case '4':   if(is)is--;
  380.                     else is=nump;
  381.                     break;
  382.         case 0:     return toupper(*p[is]);
  383.         case DN:
  384.         case RIGHT:
  385.         case '2':
  386.         case '6':   if (!p[is+1]) is=0;
  387.                     else is++;
  388.                     break;
  389.         case 3:     printm("\n");
  390.                         goto Over;
  391.         case 11:
  392.         case 27:    return 0;
  393.        default:     for (x=0;p[x]!=NULL;x++) {
  394.                         if (temp==*p[x]) {
  395.                             return toupper(*p[x]);
  396.                         }
  397.                     }
  398.                     break;
  399.     }
  400.     goto Next;
  401.  
  402. NoGraph:
  403.  
  404.     is = was = 0;
  405.     for (x = 0;p[x];x++) {
  406.         curr_cursor(&temp1,&temp2);
  407.         if((temp1 + strlen(p[x]) + 2 + 2) > 77) printm("\n");
  408.         printfm("[%c]%s  ",*p[x],&p[x][1]);
  409.     }
  410.     printm("\b\b: ");
  411.     temp = *genin(1,0,1,1,ALLL);
  412.     if(temp == '?') {
  413.         set_help("RECLINE ");
  414.         help();
  415.         goto ReStart;
  416.     }
  417.     for (x = 0;p[x];x++) {
  418.         if (temp == *p[x]) {
  419.             return toupper(*p[x]);
  420.         }
  421.     }
  422.     printm("\nInvalid choice...\n");
  423.     goto NoGraph;
  424. }
  425.